package com.cy.springcloudalibaba.services;

import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;

import java.math.BigDecimal;

/**
 * @Description: 账户
 * @ClassName: AccountService
 * @Author: chenyu
 * @Date: 2025/2/6 10:55
 */
@LocalTCC
public interface AccountService {
    /**
     * 定义两阶段提交
     * name=reduceStock为一阶段try方法
     * commitMethod = commitTcc 为二阶段提交方法
     * rollbackMethod = cancelTcc 为二阶段取消方法
     * BusinessActionContextParameter注解 可传递参数到二阶段方法
     *
     * @param userId 用户id
     * @param price  商品价格
     * @return void
     * @author chenyu
     * @date 2025/2/7 10:30
     */
    @TwoPhaseBusinessAction(name = "reduceStock", commitMethod = "commitTcc", rollbackMethod = "cancelTcc")
    void reduceBalance(@BusinessActionContextParameter(paramName = "userId") Integer userId,
                       @BusinessActionContextParameter(paramName = "price") BigDecimal price) throws Exception;

    /**
     * 确认方法，可以另命名，但要保证与commitMethod值一致
     *
     * @param context
     * @return boolean
     * @author chenyu
     * @date 2025/2/7 10:23
     */
    boolean commitTcc(BusinessActionContext context);

    /**
     * 二阶段取消方法
     *
     * @param context
     * @return boolean
     * @author chenyu
     * @date 2025/2/7 10:23
     */
    boolean cancelTcc(BusinessActionContext context);
}
